// asyncPackageComponent/components/wxml-to-canvas/wxml_to_canvas.js
const {
  poster
} = require('./demo.js')
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    shopJson: {
      type: Object,
      value: {}
    }
  },
  /**
   * shopJson = {
        poster_data,
        background
      }
   * 把海报接口的对象传进来
   * 
   */
  lifetimes: {
    attached() {
      this.widget = this.selectComponent('.widget')
    }
  },
  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    showCanvasImage() {
      let {
        wxml,
        style
      } = poster(this.data.shopJson);
      const p1 = this.widget.renderToCanvas({
        wxml,
        style
      })
      p1.then((res) => {
        this.container = res
        const p2 = this.widget.canvasToTempFilePath()
        p2.then(res => {
          this.setData({
            srcImg: res.tempFilePath,
            width: this.container.layoutBox.width,
            height: this.container.layoutBox.height
          })
          this.triggerEvent('srcImg', this.data.srcImg)
          console.log(this.data.srcImg)
          wx.showShareImageMenu({
            path:  this.data.srcImg
          });
        })
      })
    }
  }
})